﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZK.YHP_TransManage.Test
{
    public partial class 其他测试 : Form
    {
        //lock只能锁定一个引用类型变量
        private object _lock = new object();
        System.Timers.Timer timer = new System.Timers.Timer(5000);
        public 其他测试()
        {
            InitializeComponent();
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;
            timer.AutoReset = false;
            // timer.Start();

        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //throw new NotImplementedException();
            MessageBox.Show("" + timer.Enabled);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ZK.IBLL.TestIBLL testIBLL = new ZK.HardWareHelp.BLL.TestBLL();
            //var a = testIBLL.say();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("jieguo " + timer.Enabled);
            timer.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                int j = i;
                Task.Factory.StartNew(() =>
                {
                    sum1 += 1;
                    SetText(j);
                });
            }
        }

        public int sum1 = 0;
        public int sum2 = 0;
        public int sum3 = 0;
        public int sum4 = 0;

        public void SetText(int i)
        {
            sum3 += 1;
            lock (_lock)
            {
                sum4 += 1;
                this.Invoke(new Action(() =>
                   {
                       label1.Text = "this is " + i;
                       sum2 += 1;
                   }));
                System.Threading.Thread.Sleep(500);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show(
                "\n sum1 xiancheng:" + sum1
                + "\n sum2 shiji:" + sum2
                + "\n sum3 SetTextruntime:" + sum3
                + "\n sum4:" + sum4
                );
        }

        private void button5_Click(object sender, EventArgs e)
        {
            sum1 = 0;
            sum2 = 0;
            sum3 = 0;
            sum4 = 0;
        }

        System.Timers.Timer timer1 = new System.Timers.Timer(50);
        public int sum5 = 0;
        private void button6_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.AutoReset = true;
            timer1.Elapsed += Timer1_Elapsed;
        }

        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            sum5 += 1;
            this.Invoke(new Action(() =>
            {
                label2.Text = "-" + sum5;
            }));

        }

        private void button8_Click(object sender, EventArgs e)
        {
            MessageBox.Show("" + sum5);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Remove("123");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox2.Text.Trim();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show("change");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {
                throw new Exception("123");
            }
            catch (Exception ex)
            {

                return;
            }
            finally
            {
                string a = "ooo";
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {


            Task.Factory.StartNew(() =>
            {
                System.Threading.Thread.Sleep(3000);
                this.Invoke(new Action(() =>
                {
                    textBox3.Text = "";
                }));
            });

        }
    }
}
